import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';

@Injectable()
export class Httputils {
  private headers: HttpHeaders;
  constructor(private httpc: HttpClient) {
    // console.log('Hello RestProvider Provider');
    this.headers = new HttpHeaders();
    this.headers.set('Content-Type', 'application/json');
  }
  //account
  // private baseurl = 'http://10.80.5.247:8088/jt_gjgl/appinteface/';
  // private baseurl = 'http://10.80.5.187:8120/jt_gjgl/appinteface/';
  // private baseurl = 'http://58.58.27.94:8120/jt_gjgl/appinteface/';
  private baseurl = 'http://127.0.0.1:8018/';
  // private baseurl = 'http://10.80.5.247:8088/';

// 

  //account
  private apiUrlRegister = this.baseurl + 'regist.do';
  private apiUrlLogin = this.baseurl + 'user/login';


  //登录
  login(mobile, password): Observable<loginresult[]> {
    return this.httpc.get<loginresult[]>(this.apiUrlLogin + "?username=" + mobile + "&password=" + password, { headers: this.headers })
      .pipe(
        catchError(this.handleError('getCars', []))
      );
  }
  //  * 注册请求
  register(username, phone, name, password): Observable<string[]> {
    return this.httpc.get<string[]>(this.apiUrlRegister + "?username=" + username + "&phone=" + phone + "&name=" + name + "&password=" + password, { headers: this.headers })
      .pipe(
        catchError(this.handleError('getCars', []))
      );
  }
  private apifileupload ='http://192.168.216.1:8088/jngh_app/bigfile2.do';

  //  文件上传
  testdileupload(body ): Observable<string[]> {


    return this.httpc.post<string[]>(this.apifileupload ,body)
      .pipe(
        catchError(this.handleError('getCars', []))
      );
  }
//保存
saveTaskProcess(data): Observable<string[]> {
  return this.httpc.post<string[]>(this.baseurl + 'fileupload' , data)
    .pipe(
      catchError(this.handleError('nextTaskProcess', []))
    );
}

  private handleError<T>(operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {
      console.error(error); // log to console instead

      return of(result as T);
    };
  }
}
export class loginresult {
  success: string;
  userid: string;
}